/*
 * DinnerService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.onesalt.domain.Dinner;
import com.onesalt.domain.Kitchen;

/**
 * 
 *
 * @author kiri
 * @date 2015年5月9日
 * @version 1.0
 */
@Service
@Transactional
public class DinnerService extends AbstractService {

	private final DinnerRepository dinnerRepository;

	@Autowired
	public DinnerService(DinnerRepository dinnerRepository) {
		this.dinnerRepository = dinnerRepository;
	}

	public DinnerRepository getDinnerRepository() {
		return dinnerRepository;
	}
	
	public Dinner findDinner(long dinnerId) {
		Dinner dinner = getDinnerRepository().findOne(dinnerId);
		return dinner;
	}

	public List<Dinner> createDinners(Kitchen kitchen, List<Dinner> dinners) {
		List<Dinner> result = new ArrayList<Dinner>();
		for (Dinner dinner : dinners) {
			dinner.setKitchenId(kitchen.getKitchenId());
			dinner.setCreateTime(new Date());
			Dinner obj = getDinnerRepository().save(dinner);
			obj.setKitchen(kitchen);
			if (obj != null) {
				result.add(obj);
			}
		}
		return result;
	}
	
	public int cancelDinner(Long dinnerId) {
		return getDinnerRepository().setStatusFor(dinnerId, Dinner.STATUS_CANCELLED, new Date());
	}
}
